﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MethodWorx.CMS.Requests;
using MethodWorx.CMS.Content;
using System.Web.Mvc;
using MethodWorx.CMS.Services;
using MethodWorx.Core.Validation;
using MethodWorx.Core.DomainModel;
using MethodWorx.CMS.UI.Application;

namespace MethodWorx.CMS.UI.Models
{
    public class WorkflowSettingsModel : WorkflowModel
    {
        public WorkflowSettingsModel(ContentService content, WorkflowService workflow)
            : base(content, workflow)
        {
        }


        public void InitializeForEdit(Guid id, string workflowName)
        {
            base.Initialize(id);

            var workflow =
                this.page.GetWorkflowConfiguration(true).FirstOrDefault(w => w.Name.ToLower() == workflowName.ToLower());

            if(workflow == null)
                throw new BusinessRuleException("Workflow Not Found");

            this.Request = new WorkflowRequest(this.page, workflow);
        }

        public override void Complete(IValidationProvider validation)
        {
            this.workflow.SaveWorkflow(this.Request, validation);
        }
    }
}